package com.lan.mock15.c;

import java.util.*;


public class Main {
	
	private static Map<Integer, Integer> mp = new HashMap<>();
	
	private static long getNum(Long x) {
		long cnt = 0L;
		while(x > 0) {
			int t = (int) (x % 10);
			cnt += mp.get(t);
			x /= 10;
		}
		return cnt;
	}
	
	public static void main(String[] args) {
		mp.put(0, 1);
		mp.put(1, 0);
		mp.put(2, 0);
		mp.put(3, 0);
		mp.put(4, 1);
		mp.put(5, 0);
		mp.put(6, 1);
		mp.put(7, 0);
		mp.put(8, 2);
		mp.put(9, 1);
		Scanner input = new Scanner(System.in);
		
		int n = input.nextInt();
		
		Long[] p = new Long[n];
		
		for (int i = 0; i < n; i ++) {
			p[i] = input.nextLong();
		}
		
		Arrays.sort(p, new Comparator<Long>() {

			@Override
			public int compare(Long o1, Long o2) {
				long a = getNum(o1);
				long b = getNum(o2);
				if (a == b) {
					return (int) (o1 - o2);
				} else {
					return (int) (a - b);
				}
				
			}
		});
		for (long e: p) {
			System.out.print(e + " ");
		}
	}
}
